package cn.com.sbell.vss.commander;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.sbell.vss.ICommander;
import cn.com.sbell.vss.Response;
import cn.com.sbell.vss.Util;
import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.VssException;
import cn.com.sbell.vss.monitor.PerformanceMonitor;

/**
 * @author Yifu.HUANG
 * 
 */
public class InitServerAlarmConfiguration implements ICommander {
	private static final Log log = LogFactory
			.getLog(InitServerAlarmConfiguration.class);

	private PerformanceMonitor maPerformanceMonitor;

	private PerformanceMonitor csgPerformanceMonitor;

	private PerformanceMonitor emsPerformanceMonitor;

	/**
	 * (non-Javadoc)
	 * 
	 * @see cn.com.sbell.vss.ICommander#execute(javax.servlet.http.HttpServletRequest)
	 */
	public Response execute(HttpServletRequest request) {
		String serverType = Util.getStringParameter(request, "serverType",
				true, 32, "serverType");
		String[] alarm = request.getParameterValues("alarm");
		int times = Util.getIntParameter(request, "times", true, "times");
		if (VssConstants.SOURCE_TYPE_MA.equals(serverType)
				&& maPerformanceMonitor != null) {
			maPerformanceMonitor.setTimes(times);
			setThreshold(maPerformanceMonitor, alarm);
		} else if (VssConstants.SOURCE_TYPE_CSG.equals(serverType)
				&& csgPerformanceMonitor != null) {
			csgPerformanceMonitor.setTimes(times);
			setThreshold(csgPerformanceMonitor, alarm);
		} else if (VssConstants.SOURCE_TYPE_EMS.equals(serverType)
				&& emsPerformanceMonitor != null) {
			emsPerformanceMonitor.setTimes(times);
			setThreshold(emsPerformanceMonitor, alarm);
		}

		Response response = Response.createResponse(request);
		response.setReturnCode(VssConstants.CODE_SUCCESS);
		return response;
	}

	private void setThreshold(PerformanceMonitor monitor, String[] alarm) {
		String paramName = "";
		try {
			for (int i = 0; i < alarm.length; i++) {
				String[] as = alarm[i].split(",");
				paramName = as[0];
				if (as[0].endsWith("CPU")) {
					monitor.setCpuHighThreshold(Integer.parseInt(as[1]));
					monitor.setCpuLowThreshold(Integer.parseInt(as[2]));
				} else if (as[0].endsWith("DISKFULL")) {
					monitor.setDiskHighThreshold(new int[] { Integer
							.parseInt(as[1]) });
					monitor.setDiskLowThreshold(new int[] { Integer
							.parseInt(as[2]) });
				} else if (as[0].endsWith("MEM")) {
					monitor.setMemoryHighThreshold(Integer.parseInt(as[1]));
					monitor.setMemoryLowThreshold(Integer.parseInt(as[2]));
				}
			}
			if(monitor.getPerformanceMonitorThread()!=null){
				monitor.getPerformanceMonitorThread().interrupt();
			}
		} catch (Exception ex) {
			log.warn("Exception:", ex);
			throw new VssException(VssConstants.CODE_INVALID,
					(paramName == null ? "" : paramName));
		}
	}

	public void setCsgPerformanceMonitor(
			PerformanceMonitor csgPerformanceMonitor) {
		this.csgPerformanceMonitor = csgPerformanceMonitor;
	}

	public void setEmsPerformanceMonitor(
			PerformanceMonitor emsPerformanceMonitor) {
		this.emsPerformanceMonitor = emsPerformanceMonitor;
	}

	public void setMaPerformanceMonitor(PerformanceMonitor maPerformanceMonitor) {
		this.maPerformanceMonitor = maPerformanceMonitor;
	}

}
